<?php

namespace Admin\Controller;
use Think\Controller;
use Think\Log;
use Org\Util\Page; 

class SaleController extends BaseController {
	function _initialize() {
		parent::_initialize ();
	}

	public function index() {
		$msg=M("wx_sale");

		$keyword=I('keyword'); 
        $where="1=1";
        
		if($keyword)
		{
		   $where.=" and a.salename  like '%".$keyword."%'";
		}  
		 
		$where= str_replace("1=1 and", "", $where);
		$where= str_replace("1=1", "", $where);
		
	    $count =$msg->alias("a")->field("a.*,b.user_name as adminname")->join(" left join ".$this->dbprefix."admin_user b on a.operationuser=b.user_id")->where($where)->count(); // 查询满足要求的总记录数

		$Page = new Page ($count, 12 ); // 实例化分页类 传入总记录数和每页显示的记录数
		$Page -> setConfig('header', '条记录');
		$Page -> setConfig('theme', '<li><a>%totalRow% %header%</a></li> <li>%upPage%</li> <li>%downPage%</li> <li>%first%</li>  <li>%prePage%</li>  <li>%linkPage%</li>  <li>%nextPage%</li> <li>%end%</li> ');//(对thinkphp自带分页的格式进行自定义)
		$show = $Page->show (); // 分页显示输出

		$ret=$msg->alias("a")->field("a.*,b.user_name as adminname")->join(" left join ".$this->dbprefix."admin_user b on a.operationuser=b.user_id")->where($where)->limit( $Page->firstRow . ',' . $Page->listRows )->order("a.createtime desc")->select(); 
        
	
		$this->assign ( "sale", $ret );
		$this->assign ( "page", $show ); // 赋值分页输出 
		$this->display ();
	}
	
	
	public function add()
	{
		$id=I("id");
		
		$id= isset($id)?$id:0;
		$id =empty($id)?0:$id; 
		 
		if($id>0)
		 $ret= M("wx_sale")->where("saleid='$id'")->find();  
		$this->assign ( "entity", $ret ); 
		$this->display ();
	}
 
   
	public function addsale() {
		$data = $_POST; 
		$data["operationuser"] =UID;
		$data["createtime"] = date("Y-m-d H:i:s"); 
		$sid=0;
		
		$ret=$result=1;
		$db=M ("wx_sale");
		
		//$db->startTrans(); //事物导致有个更新语句不执行，提交也无效。
		
		if (empty($_POST["saleid"])||$_POST["saleid"] == 0) {
			$data ["saleid"]=getGuid();			 
			$result =$db->add ( $data ); 
		} else { 
			$result = $db->save ( $data );
		}
		
		$sid = $data ["saleid"];
		$isactive= $data['isactive'];
		
		if($isactive){
			$sqlupd="update ".$this->dbprefix."wx_sale set isactive='0' where saleid !='$sid' and isactive=1";
		    $ret=$db->execute($sqlupd);
		}
		 
		
		$exe_scriptstr = "<script type='text/javascript'>
                setTimeout(function(){
					 var win=   art.dialog.opener;
                     win.location.reload();
                     art.dialog.close();
                },300); 
           </script>";
		

		if ($result) {
			//$db->comment();
			$this->success ( "操作成功!".$exe_scriptstr);
		}
		else{
			//$db->rollback();
			$this->error ( "删除失败！".$exe_scriptstr);
		}
	}

	public function delsale(){
		$id=I("id");
		if(empty($id)){
			$this->error("传递参数错误!");
		}  
		$where="FIND_IN_SET(saleid,'$id')";
		
		$total= M("wx_salelist")->where($where)->count();
		if($total>0){
			$this->error("删除的主题中已经存在特价商品信息，请先删除商品清单记录!");
		}
		
		$result = M("wx_sale")->where($where)->delete();
		if ($result) {
			$this->success("删除成功!");
		}
		else{
			$this->error ( "删除失败！" );
		}
	}
	
	 
	public function setsale(){
	    $id=I("id");
	    //$isactive=I("isactive");
		if(empty($id)){
			$this->error("传递参数错误!");
		} 
		
		//$where="FIND_IN_SET(saleid,'$id')";
		$where="saleid='$id'";
		
		$db=M("wx_sale");
		
		$ret =M("wx_sale")->where($where)->find();
		if(!$ret){
			$this->error("记录不存在，是否被删除!");
		}  
		
		$curval= $ret['isactive']?0:1;  //设置有效无效，根据当前记录状态，更新相反的状态。
		
		//$db->startTrans(); //启动事物
		$ret1=$result=1; //默认有效
		
	    if($curval){
	    	$sqlupd="update ".$this->dbprefix."wx_sale set isactive='0' where saleid !='$id' and isactive=1";
	    	$ret1=$db->execute($sqlupd);
	    } 
	    
	    $sql="update ".$this->dbprefix."wx_sale set isactive='$curval' where saleid ='$id'";
		$result=$db->execute($sql);
		
		if ($result) {
			//$db->comment();
			$this->success("操作成功!");
		}
		else{
			//$db->rollback();
			$this->error ( "操作失败！" );
		}
	}

	/*
	public function getbyid(){
		$result = M("wx_reply")->where(array("id"=>$_POST["id"]))->find();
		if ($result) {
			$this->ajaxReturn($result);
		}
	} 
	*/

}
?>
